/**
 * zmn-供应商管理
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author DaiZhichao
 * @since 2018/10/26
 */
layui.define(['index', 'form', 'table', 'layedit', 'laydate', 'admin'], function () {
    window.$ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let table = layui.table;
    let admin = layui.admin;
    let layedit = layui.layedit;
    let laydate = layui.laydate;
    let tableRender = function (data) {
        zmnLayUtil.layTableRender(table, cols, "zmn-supplier-table",  data, "/supplier/ajax/list.action");
    };
    let tablReload = function (data) {
        table.reload("zmn-supplier-table",{ where: data });
    };
    let cols = [
        [
            {type: "checkbox", width: 50, align: 'center'},
            {field: 'supplierId', title: 'ID', width: 70, align: 'center', unresize: true},
            // {type: 'numbers', title: '序号', width: 40, align: 'center'},
            {field: 'name', title: '供应商名称', align: 'center'},
            {field: 'companyCityName', title: '城市', align: 'center'},
            {field: 'companyAddress', title: '地址', align: 'center'},
            {field: 'companyName', title: '所属公司', align: 'center'},
            {field: 'companyContact', title: '联系人', align: 'center'},
            {field: 'companyMobile', title: '手机号', align: 'center'},
            {
                field: 'useCompanyIdStr', title: '使用公司', align: 'center', templet: function (d) {
                    let useCompanyStr = "";
                    if (d.useCompanyIdStr) {
                        let useCompanyIdList = JSON.parse(d.useCompanyIdStr);
                        let useCompanyStrList = useCompanyIdList
                            .map(useCompanyId => {
                                let useCompany = Array.from(json.platformCompanySimpleList)
                                    .find(platformCompanySimple => Number(platformCompanySimple.value) === useCompanyId);
                                console.log(useCompany);
                                return (useCompany)?useCompany.text:"<b style='color: red'>无此公司</b>";
                                }
                            );
                       return useCompanyStrList.reduce((acc,useCompanyStr)=>
                           acc+useCompanyStr+","
                       ,"");
                    }
                    return useCompanyStr;
                }
            },
            {field: 'status', title: '状态', width: 60, align: 'center', unresize: true, templet: "#zmn-templet-status", sort: true},
            {fixed: 'right', title: '操作', width: 100, align: 'center', toolbar: '#zmn-supplier-table-bar'}
        ]
    ];


    //请求数据表格
    tableRender("");

    //日期
    laydate.render({
        elem: '#updateStartEndDate'
        , range: true
    });

    // 启用/停用
    let tableEnableDisable = function(tableName){
        table.on("toolbar(" + tableName + ")", function (obj) {
            let ids = table.checkStatus(tableName).data.map((item) => item.supplierId);

            if (obj.event !== "start" && obj.event !== "stop") {
                return false;
            }
            if (!ids.length > 0) {
                layer.msg('请选择要操作的供应商', {icon: 2});
                return false;
            }
            zmnLayUtil.tableEnableDisable($, obj, "/supplier/ajax/updateStatus.action", ids,function () {
                tablReload();
            });
        });
    };
    tableEnableDisable('zmn-supplier-table');

    //监听表单提交
    form.on('submit(query-submit)', function (data) {
        zmnMothedUtil.deleteEmptyProperty(data.field);
        tableRender(data.field);
        return false;
    });


});


